//
//  XcodeCleanResultVC.swift
//  ZJTools
//
//  Created by 万孟超 on 2023/11/17.
//

import Cocoa
import SnapKit

class XcodeCleanResultVC: BaseVC {
    
    lazy var titleLabel: NSTextField = {
        let tf = NSTextField()
        tf.isVerticalCenter = true
        tf.font = .systemFont(ofSize: 10)
        tf.isBordered = false
        tf.isEditable = false
        tf.textColor = "666666".color()
        tf.bezelStyle = .roundedBezel
        tf.stringValue = cleanType.titleDes()
        return tf
    }()
    
    lazy var tableView: NSTableView = {
        let table = NSTableView()
        table.headerView = nil
        table.rowHeight = 52
        table.delegate = self
        table.dataSource = self
        table.style = .automatic
        table.rowSizeStyle = .custom
        table.focusRingType = .none
        table.backgroundColor = .clear
        table.selectionHighlightStyle = .none
        let tableColumn = NSTableColumn(identifier: .init("tableColumn"))
        tableColumn.width = 608
        table.addTableColumn(tableColumn)
        return table
    }()
    
    var cleanModel: XcodeCleanManager.CleanModel! {
        didSet {
            Async(.main) {
                self.tableView.reloadData()
            }
        }
    }
        
    var cleanType: XcodeCleanManager.CleanModelType
    
    init(cleanType: XcodeCleanManager.CleanModelType) {
        self.cleanType = cleanType
        super.init(nibName: nil, bundle: nil)
        refreshData()
        installSubViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func loadView() {
        let visualEffectView = NSView()
        visualEffectView.frame = NSRect(origin: .zero, size: CGSize(width: 640, height: 650))
        view = visualEffectView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func installSubViews() {
        
        view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.right.top.equalToSuperview()
            make.height.equalTo(40)
            make.left.equalTo(12)
        }
        
        let scrollView = NSScrollView()
        scrollView.drawsBackground = false
        
        scrollView.documentView = tableView
        view.addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }
    
    func refreshData() {
        cleanModel = cleanType.obtainCleanModel()
    }
    
}

extension XcodeCleanResultVC: NSTableViewDelegate, NSTableViewDataSource {
    func numberOfRows(in tableView: NSTableView) -> Int {
        cleanModel.subPath.count
    }
    
    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        var cellView = tableView.makeView(withIdentifier: .init("XcodeCleanResultCell\(row)" + cleanType.currentTitle()), owner: self) as? XcodeCleanResultCell
        if cellView == nil {
            cellView = XcodeCleanResultCell()
            cellView?.identifier = .init("XcodeCleanResultCell\(row)" + cleanType.currentTitle())
        }
        cellView?.title = cleanModel.subPath[row].path
        cellView?.size = cleanModel.subPath[row].size
        cellView?.deleteAction = {[weak self] in
            guard let self = self else { return }
            Async(.custom(name: "removeFile")) {
                let filePath = self.cleanModel.path + "/" + self.cleanModel.subPath[row].path
                File.removePath(filePath) {
                    if $0 {
                        self.cleanModel.size -= self.cleanModel.subPath[row].size
                        self.cleanModel.subPath.remove(at: row)
                        self.cleanType.freshManagerData(self.cleanModel)
                    }
                }
            }
        }
        cellView?.showInFinderBlock = {[weak self] in
            guard let self = self else { return }
            let filePath = self.cleanModel.path + "/" + self.cleanModel.subPath[row].path
            NSWorkspace.shared.selectFile(filePath, inFileViewerRootedAtPath: "")
        }
        return cellView
    }
    
}
